
package com.wufun.overseas.tools;

import android.text.TextUtils;

public final class SafeMethods {
    /**
     * Safely converts a string into a boolean.
     *
     * @param val  String containing the boolean to convert.
     * @param fail Value to use if unable to convert val to a boolean.
     * @return The converted boolean, or the specified value if unsuccessful.
     */
    public static boolean toBoolean(String val, boolean fail) {
        if (TextUtils.isEmpty(val))
            return fail; // Not a boolean

        try {
            // Convert to boolean
            return Boolean.parseBoolean(val);
        } catch (NumberFormatException nfe) {
        }
        // Conversion failed
        return fail;
    }



    /**
     * Safely sleep.
     *
     * @param milliseconds The sleep duration.
     */
    public static void sleep(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
        }
    }

    /**
     * Safely wait for a thread to die.
     *
     * @param thread       Thread to join.
     * @param milliseconds Time to wait, in milliseconds (0 to wait indefinitely).
     */
    public static void join(Thread thread, int milliseconds) {
        if (thread == null || milliseconds < 0)
            return;

        try {
            thread.join(milliseconds);
        } catch (InterruptedException e) {
        }
    }


}
